package com.dswz.controller.pc;

import com.dswz.po.Goods;
import com.dswz.po.User;
import com.dswz.po.UserCart;
import com.dswz.service.GoodsService;
import com.dswz.service.SessionService;
import com.dswz.service.UserCartService;
import com.dswz.service.UserService;
import com.dswz.utils.AdminResult;
import com.dswz.utils.PageBean;
import com.dswz.utils.RequestUtils;
import com.dswz.utils.Utils;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({ "/pcs" })
public class UserCartPcController {
	private Logger logger = LogManager.getLogger(UserCartPcController.class);

	@Autowired
	private UserCartService userCartService;

	@Autowired
	private UserService userService;

	@Autowired
	private GoodsService goodsService;

	@Autowired
	private SessionService sessionService;

	@RequestMapping(value = { "/getUserCartList" }, method = {
			org.springframework.web.bind.annotation.RequestMethod.GET })
	@ResponseBody
	public Object getUserCartList(String userId, Integer page, Integer rows, Model model, HttpServletRequest request,
			HttpServletResponse response) {
		long startTime = System.currentTimeMillis();
		AdminResult result = new AdminResult();
		try {
			String username = this.sessionService.getAttibute(RequestUtils.getCSESSIONID(request, response),
					"USER_SESSION");
			this.logger.info("userId:" + userId + "page:" + page + "limit:" + rows + "页面来源:"
					+ request.getHeader("referer") + "客户端:" + request.getHeader("user-agent") + " 客户端ip: "
					+ Utils.getUserRealAddress(request) + " 操作人: " + username);
			PageBean pb = this.userCartService.getUserCartList(username, userId, Byte.valueOf((byte) 0), page, rows);
			result.setResult(0);
			result.setMessage("OK");
			result.setRows(pb.getContentList());
			result.setTotal(pb.getTotalRecords());
			long endTime = System.currentTimeMillis();
			this.logger.info("result:" + new Gson().toJson(result) + "程序运行时间" + (endTime - startTime) + "ms");
		} catch (Exception e) {
			result.setResult(2);
			result.setMessage(e.getMessage());
			this.logger.error(e.getMessage(), e);
		}
		return result;
	}

	@RequestMapping(value = { "/saveAddUserCart" }, method = {
			org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public Object saveAddUserCart(String userId, String goodsId, Integer userCartBuyNum, Model model,
			HttpServletRequest request, HttpServletResponse response) {
		long startTime = System.currentTimeMillis();
		AdminResult result = new AdminResult();
		try {
			String username = this.sessionService.getAttibute(RequestUtils.getCSESSIONID(request, response),
					"USER_SESSION");
			this.logger.info("userId:" + userId + "goodsId:" + goodsId + "userCartBuyNum:" + userCartBuyNum + "页面来源:"
					+ request.getHeader("referer") + "客户端:" + request.getHeader("user-agent") + " 客户端ip: "
					+ Utils.getUserRealAddress(request) + " 操作人: " + username);
			if (StringUtils.isEmpty(userId)) {
				this.logger.error("请输入用户id!");
				result.setMessage("请输入用户id!");
				result.setResult(3);
				return result;
			}
			if (StringUtils.isEmpty(goodsId)) {
				this.logger.error("请输入商品id!");
				result.setMessage("请输入商品id!");
				result.setResult(3);
				return result;
			}
			if (userCartBuyNum == null) {
				this.logger.error("价格为空!");
				result.setMessage("价格为空!");
				result.setResult(3);
				return result;
			}
			User user = this.userService.getUserById(userId);
			Goods goods = this.goodsService.getGoodsById(goodsId);
			UserCart userCart = new UserCart();
			userCart.setUserCartId(UUID.randomUUID().toString().replaceAll("-", ""));
			userCart.setGoodsId(goodsId);
			userCart.setGoodsName(goods.getGoodsTitle());
			userCart.setGoodsPic(goods.getGoodsPic());
			userCart.setGoodsPrice(goods.getGoodsPrice());
			userCart.setUserCartBuyNum(userCartBuyNum);
			userCart.setUserId(userId);
			userCart.setUserMobile(user.getUserMobile());
			userCart.setUserName(user.getUserName());
			this.userCartService.insertUserCart(userCart);
			result.setResult(0);
			result.setMessage("OK");
			long endTime = System.currentTimeMillis();
			this.logger.info("result:" + new Gson().toJson(result) + "程序运行时间" + (endTime - startTime) + "ms");
		} catch (Exception e) {
			this.logger.error(e.getMessage(), e);
			result.setMessage(e.getMessage());
			result.setResult(2);
		}
		return result;
	}

	@RequestMapping({ "/deleteUserCarts" })
	@ResponseBody
	public Object deleteUserCarts(String userCartIds, Model model, HttpServletRequest request,
			HttpServletResponse response) {
		long startTime = System.currentTimeMillis();
		AdminResult result = new AdminResult();
		try {
			String username = this.sessionService.getAttibute(RequestUtils.getCSESSIONID(request, response),
					"USER_SESSION");
			this.logger.info("userCartIds:" + userCartIds + "页面来源:" + request.getHeader("referer") + "客户端:"
					+ request.getHeader("user-agent") + " 客户端ip: " + Utils.getUserRealAddress(request) + " 操作人: "
					+ username);
			List<String> list = new ArrayList();
			if (StringUtils.isEmpty(userCartIds)) {
				this.logger.error("请选择一个");
				result.setMessage("请选择一个");
				result.setResult(3);
				return result;
			}
			String[] userCartIdArr = userCartIds.split(",");
			String[] arrayOfString1;
			int j = (arrayOfString1 = userCartIdArr).length;
			for (int i = 0; i < j; i++) {
				String userCartId = arrayOfString1[i];
				list.add(userCartId);
			}
			this.userCartService.deleteUserCarts(list);
			result.setResult(0);
			result.setMessage("OK");
			long endTime = System.currentTimeMillis();
			this.logger.info("result:" + new Gson().toJson(result) + "程序运行时间" + (endTime - startTime) + "ms");
		} catch (Exception e) {
			this.logger.error(e.getMessage(), e);
			result.setMessage(e.getMessage());
			result.setResult(2);
		}
		return result;
	}
}
